<?php
declare (strict_types=1);

namespace addons\BuilderPlus\contract\event;

use app\common\model\Logs as LogsModel;
use addons\BuilderPlus\exception\BuilderException;

class Logs
{
    public function __construct($data)
    {
        try {
            $type = $data['type'];
            return $this->$type($data['param'], $data['arguments'] ?? '');
        } catch (\Exception $e) {
            throw new BuilderException("日志记录失败：" . $e->getMessage());
        }
    }

    private function update($param, $arguments)
    {

        $msg = "修改【{$arguments['menu']['s_nav']}】-> ";
        foreach ($param['update'] as $key => $val) {
            $match = $this->matchCol($key, $arguments['columns']);
            if (!empty($match['switch'])) {
                $text = explode('|', $match['switch']['text']);
                $switchVal = explode('|', $match['switch']['val']);
                $front = $text[array_search($param['rowData'][$key], $switchVal)];
                $after = $text[array_search($val, $switchVal)];
            } else {
                $front = $param['rowData'][$key];
                $after = $val;
            }
            if (empty($match)) continue;
            $msg .= "[ {$match['title']} ]由[ {$front} ]修改为[ {$after} ]记录ID【{$param['id']}】";
        }
        if ($msg) {
            LogsModel::adds($msg, request()->admin->id, request()->admin->admin_name);
        }
    }

    private function delete($param, $arguments)
    {
        $msg = "删除【{$arguments['menu']['s_nav']}】-> 记录ID【{$param['id']}】";
        LogsModel::adds($msg, request()->admin->id, request()->admin->admin_name);
    }

    private function save($param)
    {
        try {
            $msg = [];
            foreach ($param['justData'] as $key => $datum) {
                if ($datum['originalVal'] != $datum['val']) {
                    if (!empty($datum['dataType']) && $datum['dataType'] == 'ratio') {
                        $front = $datum['originalVal'] . '%';
                        $after = ($datum['val'] * 100) . '%';
                    } else {
                        $front = $datum['originalTextVal'] ?: $datum['originalVal'];
                        $after = $datum['textVal'] ?: $datum['val'];
                    }
                    $msg[] = $datum['title'] . "由[ {$front} ]修改为[ {$after} ]";
                }
            }
            if ($msg) {
                $code = '';
                if (!empty($param['data']['code'])) {
                    $code = "编号【{$param['data']['code']}】";
                }

                LogsModel::adds("修改【{$param['nav']}】：" . implode('，', $msg) . '；' . "ID【{$param['data']['id']}】" . $code,
                    request()->admin->id, request()->admin->admin_name);
            }

        } catch (\Exception $e) {

            throw new BuilderException($e->getMessage());
        }
    }

    private function matchCol($filed, $columns): array
    {
        foreach ($columns as $column) {
            if ($column['field'] == $filed) return $column;
        }
        return [];
    }
}